﻿using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.DataAccess;

namespace AMSInventoryWeb.UserControls
{
    public partial class ChildObjectDdlCtl : UserControl
    {
        private BaseDataAccess m_Access = new BaseDataAccess();
        private PageLoadHelper m_LoadHelper = new PageLoadHelper("");

        public bool Enabled
        {
            set { ddlObjectKeyInfo.Enabled = value; }
            get { return ddlObjectKeyInfo.Enabled; }
        }

        public string Width
        {
            get { return hdnWidthVal.Value; }
            set
            {
                hdnWidthVal.Value = value;
                if (value != null)
                {
                    ddlObjectKeyInfo.Width = Unit.Parse(value);
                }
            }
        }

        public string ObjClassName
        {
            get { return hdnClassName.Value; }
            set { hdnClassName.Value = value; }
        }

        public string ObjTypeName
        {
            get { return hdnTypeName.Value; }
            set { hdnTypeName.Value = value; }
        }

        public string ObjKeyProName
        {
            get { return hdnKeyProName.Value; }
            set { hdnKeyProName.Value = value; }
        }

        public string ObjShowProName
        {
            get { return hdnShowProName.Value; }
            set { hdnShowProName.Value = value; }
        }

        public object ObjKeyProVal
        {
            get { return ddlObjectKeyInfo.SelectedValue; }
            set
            {
                if (value != null)
                {
                    hdnKeyProVal.Value = m_LoadHelper.GetProObjStringValue(ObjKeyProName, value);
                    ddlObjectKeyInfo.SelectedValue = hdnKeyProVal.Value;
                }
            }
        }

        public string ObjShowProVal
        {
            get { return ddlObjectKeyInfo.SelectedItem.Text; }
        }

        private void SetData()
        {
            ddlObjectKeyInfo.Items.Clear();
            string strTypeNameCondition = "";
            if (ObjTypeName.Length > 0)
                strTypeNameCondition = " where o.TypeName='" + ObjTypeName + "'";
            string hql = "Select o." + ObjKeyProName + ",o." + ObjShowProName + " From " + ObjClassName + " o " +
                         strTypeNameCondition + " order by o." + ObjShowProName;
            IList lstKeyInfo = m_Access.Session.CreateQuery(hql).List();
            if (lstKeyInfo != null)
            {
                foreach (object[] objs in lstKeyInfo)
                {
                    if (objs != null && objs.Length == 2)
                    {
                        string strKeyVal = objs[0].ToString();
                        string strShowVal = objs[1].ToString();
                        ddlObjectKeyInfo.Items.Add(new ListItem(strShowVal, strKeyVal));
                    }
                }
            }
            if (hdnKeyProVal.Value.Length > 0)
            {
                string keyVal = hdnKeyProVal.Value;
                ddlObjectKeyInfo.SelectedValue = keyVal;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetData();
            }
        }

        protected void ddlObjectKeyInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            hdnKeyProVal.Value = ddlObjectKeyInfo.SelectedValue;
        }
    }
}